<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-forms" layout="column">
  <application-header application="$ctrl.application"></application-header>
  <div class="gv-form">
    <h2>Members</h2>
    <div class="gv-form-content" layout="column">

        <md-table-container ng-show="$ctrl.members.length">
          <table md-table class="gv-table-dense">
            <thead md-head md-order="order">
            <tr md-row>
              <th md-column width="1%" nowrap></th>
              <th md-column order-by="lastname">Member</th>
              <th md-column order-by="type">Role</th>
              <th md-column></th>
            </tr>
            </thead>
            <tbody md-body>
            <tr md-row ng-repeat="member in $ctrl.members | orderBy: order">
              <td md-cell>
                <gravitee-identity-picture 
                  image-id="member.id"
                  image-url="$ctrl.getMembershipAvatar(member)"
                  image-name="$ctrl.getMembershipDisplay(member)"
                  image-width="40"
                  image-border-radius="true"
                  class="md-avatar"
                ></gravitee-identity-picture>
              </td>
              <td md-cell>
                <md-list-item class="md-2-line contact-item">
                  <div class="md-list-item-text compact">
                    <span ng-if="member.role == 'PRIMARY_OWNER'"><b>{{$ctrl.getMembershipDisplay(member)}}</b></span>
                    <span ng-if="member.role != 'PRIMARY_OWNER'">{{$ctrl.getMembershipDisplay(member)}}</span>
                  </div>
                </md-list-item>
              </td>
              <td md-cell permission permission-only="'application-member-u'">
                <md-select ng-model="member.role" ng-change="$ctrl.updateMember(member)" ng-disabled="member.role == 'PRIMARY_OWNER'" aria-label="Update member role">
                  <md-option ng-value="role.name" ng-repeat="role in $ctrl.roles" ng-disabled="role.system">{{role.name}}</md-option>
                </md-select>
              </td>
              <td md-cell permission permission-except="'application-member-u'">
                <span>{{member.role}}</span>
              </td>
              <td md-cell>
                <div>
                  <ng-md-icon ng-if="member.role != 'PRIMARY_OWNER'"
                              ng-click="$ctrl.showDeleteMemberConfirm($event, member)" icon="delete"
                              size="24" style="cursor: pointer;"
                              permission permission-only="'application-member-d'"></ng-md-icon>
                </div>
              </td>
            </tr>
            </tbody>
          </table>
        </md-table-container>
        <gravitee-empty-state ng-if="!$ctrl.members.length"
                              icon="group"
                              model="Member"
                              message="Members will appear here"
                              sub-message="Start attaching a member"
                              create-mode="true"></gravitee-empty-state>

        <div ng-style="!$ctrl.members.length && {'text-align':'center'}">
          <md-button  permission permission-only="'application-member-c'" ng-class="{'md-fab-bottom-right': $ctrl.members.length}" class="md-fab" aria-label="Add" ng-click="$ctrl.showAddMemberModal($event)">
            <ng-md-icon icon="add"></ng-md-icon>
          </md-button>
        </div>
    </div>
  </div>

  <div class="gv-form" ng-if="$ctrl.isAllowedToTransferOwnership()" permission permission-only="'application-member-u'">
    <h2>Transfer ownership</h2>
    <div class="gv-form-content" layout="column">

          <p>
            Give full access to this application to an other user.
          </p>
            <md-autocomplete
                    md-selected-item="selectedItem"
                    md-search-text="searchText"
                    md-search-text-change="searchTextChange(searchText)"
                    md-selected-item-change="$ctrl.selectedUserChange(user)"
                    md-items="user in $ctrl.searchUser(searchText)"
                    md-item-text="user.displayName"
                    placeholder="Select a user"
                    md-autofocus="false"
                    md-min-length="0"
                    md-menu-class="autocomplete-custom-template">
              <md-item-template>
                <md-list-item class="md-2-line contact-item">
                  <div class="md-list-item-text compact">
                    <span style="font-size: medium;">{{user.displayName}}</span>
                    <span style="padding-left: 24px; font-size: small;" ng-if="user.email">{{user.email}}</span>
                  </div>
                </md-list-item>
              </md-item-template>
              <md-not-found>
                No user matching "{{searchText}}" were found.
              </md-not-found>
            </md-autocomplete>

            <md-input-container>
              <label>New Role for the current Primary Owner</label>
              <md-select ng-model="$ctrl.newPORole">
                <md-option ng-value="role" ng-repeat="role in $ctrl.newPORoles">{{role.name}}</md-option>
              </md-select>
            </md-input-container>
      <div class="md-actions gravitee-api-save-button" layout="row">
          <md-button ng-disabled="$ctrl.newPrimaryOwner == null" class="md-raised md-primary" ng-click="$ctrl.showTransferOwnershipConfirm($event)">Transfer</md-button>
      </div>
    </div>
  </div>

  <div class="gv-form" ng-repeat="grp in $ctrl.groupIdsWithMembers">
    <h2>{{$ctrl.groupMembers[grp].length}} members inherited from "{{$ctrl.groupById[grp].name}}"</h2>
    <div class="gv-form-content" layout="column">
      <div layout="row" layout-align="start end">
        <div style="margin-bottom: 5px;" ng-model="$ctrl.displayGroups[grp]"
             ng-if="!$ctrl.displayGroups[grp]" ng-click="$ctrl.displayGroups[grp]=true">
          <a>Show</a>
        </div>
        <div style="margin-bottom: 5px;" ng-model="$ctrl.displayGroups[grp]"
             ng-if="$ctrl.displayGroups[grp]" ng-click="$ctrl.displayGroups[grp]=false">
          <a>Hide</a>
        </div>
      </div>
      <md-content style="background: none" ng-if="$ctrl.displayGroups[grp]">
        <md-table-container ng-show="$ctrl.groupMembers[grp].length">
          <table md-table class="gv-table-dense">
            <thead md-head md-order="order">
            <tr md-row>
              <th md-column order-by="username">Member</th>
              <th md-column order-by="type">Role</th>
            </tr>
            </thead>
            <tbody md-body>
            <tr md-row ng-repeat="member in $ctrl.groupMembers[grp] | orderBy: order">
              <td md-cell>{{$ctrl.getMembershipDisplay(member)}}</td>
              <td md-cell>{{member.roles["APPLICATION"]}}</td>
            </tr>
            </tbody>
          </table>
        </md-table-container>
      </md-content>
    </div>
  </div>
</div>

